import java.applet.*;
import java.awt.*;
import java.lang.*;

public class ColouredBalls extends Applet implements Runnable
{
 private int rl,rh,num;
 private Color c,cbkg;
 boolean frozen=false;
 private Graphics g;
 Thread animatorThread;
 int[] x=new int[11];
 int[] xx=new int[11];
 int[] y=new int[11];
 int[] yy=new int[11];
 int[] cr=new int[11];
 int[] cg=new int[11];
 int[] cb=new int[11];

 public void init()
{
 num=10;
 rl=350;
 rh=327;
 for (int i=1;i<=num;i++)
{
 x[i]=(int)Math.round(Math.random()*(rl-20)+10);
 xx[i]=(int)Math.round(Math.random()*5);
 y[i]=(int)Math.round(Math.random()*(rh-20)+10);
 yy[i]=(int)Math.round(Math.random()*5);
 cr[i]=(int)Math.round(Math.random()*255);
 cg[i]=(int)Math.round(Math.random()*255);
 cb[i]=(int)Math.round(Math.random()*255);
}
 cbkg=Color.white;
// c=Color.blue; 
}
 
 public void start()
{
 if (!frozen)
{ 
 if (animatorThread == null) animatorThread = new Thread(this);
 animatorThread.start();
} 
}
 
 public void stop()
{
 animatorThread=null;
}
    
 public boolean mouseDown(Event e, int x, int y) 
{
  if (frozen) 
{
     frozen = false;
     start();
}
 else 
{
     frozen = true;
     stop();
}
 return true;
} 

 public void paint(Graphics g)
{
 g.setColor(cbkg);
 g.fillRect(0,0,rl,rh);
 for(int i=1;i<=num;i++)
{
 g.setColor(new Color(cr[i],cg[i],cb[i]));
 g.fillOval(x[i],y[i],10,10);
}
}
 
 public void run()
{
  while (Thread.currentThread()==animatorThread)
{
  paint(this.getGraphics());
  for (int i=1;i<=num;i++)
{
  if ( x[i]<(rl-10) && x[i] >10) x[i]+=xx[i];
  else {xx[i]*=-1;x[i]+=xx[i];}  
  if ( y[i]<(rh-10) && y[i] >10) y[i]+=yy[i];
  else {yy[i]*=-1;y[i]+=yy[i];}  
}
  for (int j=1;j<1000000;j++);
}
}

} 
